package com.ai.spring.common.util;

import com.ai.spring.common.enums.ExceptionEnum;
import com.ai.spring.common.enums.ResponseCodeEnum;
import lombok.Data;

/**
 * 结果返回工具类
 *
 * @author 石头
 * @Date 2019/10/28
 * @Version 1.0
 **/
@Data
public final class ResponseResultUtil {
    /**
     * 判断返回是否成功
     * @param result
     * @param <T>
     * @return
     */
    public static <T>boolean isSuccess(ResponseResult<T> result){
        if (result!=null && ResponseCodeEnum.SUCCESS_CODE.getCode().equals(result.getCode())){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    /**
     * 成功结果
     * @param data
     * @param <T>
     * @return
     */
    public static <T>ResponseResult<T> ofSuccess(T data){
        return new ResponseResult(data, ResponseCodeEnum.SUCCESS_CODE.getCode());
    }
    /**
     * 失败结果
     * @param msg
     * @author 石头
     * @date 2019/10/28
     * @return com.ai.spring.common.util.ResponseResult<T>
     **/
    public static <T>ResponseResult<T> ofError(String msg){
        return new ResponseResult(msg, ResponseCodeEnum.ERROR_CODE.getCode());
    }
    /**
     * 失败结果
     * @param exceptionEnum
     * @author 石头
     * @date 2019/10/28
     * @return com.ai.spring.common.util.ResponseResult<T>
     **/
    public static <T>ResponseResult<T> ofError(ExceptionEnum exceptionEnum){
        return new ResponseResult(exceptionEnum.getName(), exceptionEnum.getCode());
    }
    /**
     * 失败结果
     * @param code
     * @author 石头
     * @date 2019/10/28
     * @return com.ai.spring.common.util.ResponseResult<T>
     **/
    public static <T>ResponseResult<T> ofError(Integer code,String msg){
        return new ResponseResult(msg, code);
    }
}
